<?php

namespace App\Console\Commands;

use App\Models\CapitalLog;
use App\Models\Member;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class MemberDay extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'total:memberday';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '统计用户每日收益';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
        $today = date('Y-m-d');
//        $today = '2018-09-04';
        $yesterday = date('Y-m-d', strtotime(" $today -1 day"));
        Member::with(['g_task_config_log'=>function($query) use($yesterday){
            return $query->select('id','task_id','date','member_id')->where('date', $yesterday);
        }])
            ->with('g_task_test_log')
            ->select('id')->chunk(100, function($list) use($yesterday){
            $memberday_insert = [
                'member_id' => '',
                'date' => $yesterday,
                'ip' => 0,
                'new_register' => 0,
                'invite' => 0,
                'invite_1' => 0,
                'child' => 0,
                'shared' => 0,
                'view' => 0,
                'test' => 0,
                'checkin' => 0
            ];
            foreach ($list as $k=>$v) {
                $memberday_insert['member_id'] = $v->id;
                $task_val = [];
                foreach ($v->g_task_config_log as $k_g=>$v_g) {
                    $total_task = CapitalLog::selectRaw('sum(money) as money, sum(money_cash) as money_cash')
                        ->where('type_id', $v_g->task_id)
                        ->where('date',$yesterday)
                        ->first()->toArray();
                    $task_val[$v_g->task_id] = $total_task;
                }

                $task_test_val = [];
                foreach ($v->g_task_test_log as $k_t=>$v_t) {
                    $task_test_val[] = $v_t->task_id;
                }
                $total_task_test = CapitalLog::selectRaw('sum(money) as money, sum(money_cash) as money_cash')
                    ->whereIn('type_test_id', $task_test_val)
                    ->where('date', $yesterday)
                    ->first()->toArray();

                if(!empty($total_task_test)) {
                    $memberday_insert['test'] += $total_task_test['money'];
                }
                //任务
                if(isset($task_val[1])) {
                    $memberday_insert['checkin'] += $task_val[1]['money'];
                }
                if(isset($task_val[2])) {
                    $memberday_insert['invite_1'] += $task_val[2]['money_cash'];
                }
                if(isset($task_val[3])) {
                    $memberday_insert['invite'] += $task_val[3]['money'];
                }
                if(isset($task_val[4])) {
                    $memberday_insert['invite'] += $task_val[4]['money'];
                }
                if(isset($task_val[5])) {
                    $memberday_insert['view'] += $task_val[5]['money'];
                }
                if(isset($task_val[6])) {
                    $memberday_insert['view'] += $task_val[6]['money'];
                }
                if(isset($task_val[7])) {
                    $memberday_insert['shared'] += $task_val[7]['money'];
                }
                if(isset($task_val[8])) {
                    $memberday_insert['shared'] += $task_val[8]['money'];
                }
                if(isset($task_val[9])) {
                    $memberday_insert['shared'] += $task_val[9]['money'];
                }
                if(isset($task_val[10])) {
                    $memberday_insert['shared'] += $task_val[10]['money'];
                }
                if(isset($task_val[0])){
                    $memberday_insert['child'] += $task_val[10]['child'];
                }
                try{
                    $res = DB::table('total_memberday')->insert($memberday_insert);
                }catch (\Exception $e) {
                    //todo 写入日志..

                }
            }
        });

    }
}
